const path = require('path');
const OSS = require('ali-oss');
const fs = require('fs').promises;

async function clearOss(store) {
  while (true) {
    const list = await store.list({ 'max-keys': 1000 });
    const names = [];
    if (!list.objects) break;
    for (const l of list.objects) {
      names.push(l.name);
    }
    await store.deleteMulti(names);
  }
}

async function getFilesForOss(rootDir) {
  const result = [];
  async function listDir(dir) {
    const files = await fs.readdir(dir);
    for (const f of files) {
      const absolutePath = path.join(dir, f);
      const stat = await fs.lstat(absolutePath);
      if (stat.isDirectory()) {
        await listDir(absolutePath);
      } else {
        result.push({
          name: path.relative(rootDir, absolutePath),
          path: absolutePath,
        });
      }
    }
  }

  await listDir(rootDir);
  return result;
}

async function putDir(store, dir, root = '') {
  const list = await getFilesForOss(dir);
  console.log(list);
}

async function main() {
  const store = new OSS({
    region: 'oss-cn-beijing',
    accessKeyId: 'a',
    accessKeySecret: 'b',
    bucket: 'sslk-test-hema',
  });
  // clearOss(store);
  putDir(store, 'D:/temp/a');
}

main();
